package com.hyphenate.easeim.section.discover;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMapOptions;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.utils.CoordinateConverter;
import com.hyphenate.easeim.R;
import com.hyphenate.easeim.section.base.BaseInitFragment;
import com.hyphenate.easeui.ui.EaseBaiduMapActivity;

public class DiscoverFragment extends BaseInitFragment {

    @Override
    protected int getLayoutId() {
        return R.layout.demo_fragment_discover;
    }


    private MapView mapView;
    private BaiduMap baiduMap;
    private BDLocation lastLocation;
    protected double latitude;
    protected double longtitude;
    protected String address;
    private BaiduSDKReceiver mBaiduReceiver;
    private LocationClient mLocClient;

    public static void actionStartForResult(Fragment fragment, int requestCode) {
        Intent intent = new Intent(fragment.getContext(), EaseBaiduMapActivity.class);
        fragment.startActivityForResult(intent, requestCode);
    }

    public static void actionStartForResult(Activity activity, int requestCode) {
        Intent intent = new Intent(activity, EaseBaiduMapActivity.class);
        activity.startActivityForResult(intent, requestCode);
    }

    public static void actionStart(Context context, double latitude, double longtitude, String address) {
        Intent intent = new Intent(context, EaseBaiduMapActivity.class);
        intent.putExtra("latitude", latitude);
        intent.putExtra("longtitude", longtitude);
        intent.putExtra("address", address);
        context.startActivity(intent);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //initialize SDK with context, should call this before setContentView
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        mapView = findViewById(R.id.bmapView);
        baiduMap = mapView.getMap();
        baiduMap.setMapStatus(MapStatusUpdateFactory.zoomTo(15.0f));
        mapView.setLongClickable(true);
        initIntent();
        initData();
    }

    private void initIntent() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            latitude = bundle.getDouble("latitude", 0);
            longtitude = bundle.getDouble("longtitude", 0);
            address = bundle.getString("address");
        }
    }

    @Override
    public void initData() {
        if (latitude == 0) {
            mapView = new MapView(getContext(), new BaiduMapOptions());
            baiduMap.setMyLocationConfigeration(
                new MyLocationConfiguration(MyLocationConfiguration.LocationMode.NORMAL, true, null));
            showMapWithLocationClient();
        } else {
            LatLng lng = new LatLng(latitude, longtitude);
            mapView = new MapView(getContext(),
                new BaiduMapOptions().mapStatus(new MapStatus.Builder().target(lng).build()));
            showMap(latitude, longtitude);
        }
        IntentFilter iFilter = new IntentFilter();
        iFilter.addAction(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR);
        iFilter.addAction(SDKInitializer.SDK_BROADCAST_ACTION_STRING_NETWORK_ERROR);
        mBaiduReceiver = new BaiduSDKReceiver();
        getActivity().registerReceiver(mBaiduReceiver, iFilter);
    }

    protected void showMapWithLocationClient() {
        mLocClient = new LocationClient(getActivity());
        mLocClient.registerLocationListener(new EaseBDLocationListener());
        LocationClientOption option = new LocationClientOption();
        // open gps
        option.setOpenGps(true);
        // option.setCoorType("bd09ll");
        // Johnson change to use gcj02 coordination. chinese national standard
        // so need to conver to bd09 everytime when draw on baidu map
        option.setCoorType("gcj02");
        option.setScanSpan(30000);
        option.setAddrType("all");
        option.setIgnoreKillProcess(true);
        mLocClient.setLocOption(option);
        if (!mLocClient.isStarted()) {
            mLocClient.start();
        }
    }

    protected void showMap(double latitude, double longtitude) {
        LatLng lng = new LatLng(latitude, longtitude);
        CoordinateConverter converter = new CoordinateConverter();
        converter.coord(lng);
        converter.from(CoordinateConverter.CoordType.COMMON);
        LatLng convertLatLng = converter.convert();
        OverlayOptions ooA = new MarkerOptions().position(convertLatLng).icon(BitmapDescriptorFactory
            .fromResource(R.drawable.ease_icon_marka))
            .zIndex(4).draggable(true);
        baiduMap.addOverlay(ooA);
        MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(convertLatLng, 17.0f);
        baiduMap.animateMapStatus(u);
    }

    public void onReceiveBDLocation(BDLocation bdLocation) {
        if (bdLocation == null) {
            return;
        }
        if (lastLocation != null) {
            if (lastLocation.getLatitude() == bdLocation.getLatitude() && lastLocation.getLongitude() == bdLocation.getLongitude()) {
                Log.d("map", "same location, skip refresh");
                // mMapView.refresh(); //need this refresh?
                return;
            }
        }
        lastLocation = bdLocation;
        baiduMap.clear();
        showMap(lastLocation.getLatitude(), lastLocation.getLongitude());
    }

//    private void sendLocation() {
//        Intent intent = getIntent();
//        intent.putExtra("latitude", lastLocation.getLatitude());
//        intent.putExtra("longitude", lastLocation.getLongitude());
//        intent.putExtra("address", lastLocation.getAddrStr());
//        intent.putExtra("buildingName", lastLocation.getBuildingName() == null ? "" : lastLocation.getBuildingName());
//        this.setResult(RESULT_OK, intent);
//        finish();
//    }

    @Override
    public void onResume() {
        mapView.onResume();
        if (mLocClient != null) {
            if (!mLocClient.isStarted()) {
                mLocClient.start();
            }
        }
        super.onResume();
    }

    @Override
    public void onPause() {
        mapView.onPause();
        if (mLocClient != null) {
            mLocClient.stop();
        }
        super.onPause();
        lastLocation = null;
    }

    @Override
    public void onDestroy() {
        if (mLocClient != null)
            mLocClient.stop();
        mapView.onDestroy();
        getActivity().unregisterReceiver(mBaiduReceiver);
        super.onDestroy();
    }

    public class BaiduSDKReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (TextUtils.equals(action, SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR)) {
                showErrorToast(getResources().getString(R.string.please_check));
            } else if (TextUtils.equals(action, SDKInitializer.SDK_BROADCAST_ACTION_STRING_NETWORK_ERROR)) {
                showErrorToast(getResources().getString(R.string.Network_error));
            }
        }
    }

    public class EaseBDLocationListener implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
            onReceiveBDLocation(bdLocation);
        }
    }

    /**
     * show error message
     *
     * @param message
     */
    protected void showErrorToast(String message) {
        Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
    }

}
